package com.shicijiaoliu.controller;

import com.shicijiaoliu.pojo.Shicitabel;
import com.shicijiaoliu.pojo.result;
import com.shicijiaoliu.service.Authorservice;
import com.shicijiaoliu.service.Shiciservice;
import com.shicijiaoliu.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/shici")
@Validated
@CrossOrigin
public class shicicontroller {

    @Autowired
    private Shiciservice shiciservice;

    @Autowired
    private Authorservice authorservice;


    @PostMapping("/usershiciadd")
    public result useraddshici(String addtitle,String addpara){
        System.out.println("addtitle:"+addtitle);
        Shicitabel shici = new Shicitabel();
        shici.setParagraphs(addpara);
        shici.setTitle(addtitle);
        if(shiciservice.findByparagraphs(shici.getParagraphs()) == null){
            Map<String,Object> claims = ThreadLocalUtils.getThreadLocal();
            String username = (String) claims.get("username");
            shici.setAuthor(username);
            shici.setSubject("现代诗");
            shici.setDynasty("现代");
            shici.setAuthorId(authorservice.findByname(username).getId());
            shiciservice.useraddshici(shici);
            return result.success();
        }else {
            return result.error("诗词内容已创作，请重新编写");
        }
    }

    @GetMapping("/find/{title}")
    public result find(@PathVariable("title")String title){
        List<Shicitabel> slist= shiciservice.findBytitle2(title);
        Map<String,Object> data = new HashMap<>();
        data.put("total",slist.size());
        data.put("items",slist);
        return result.success(data);
    }

    @GetMapping("/find/name/{title}")
    public result findname(@PathVariable("title")String title){
        List<Shicitabel> slist= shiciservice.findByname2(title);
        Map<String,Object> data = new HashMap<>();
        data.put("total",slist.size());
        data.put("items",slist);
        return result.success(data);
    }

    @GetMapping("/find/content/{title}")
    public result findcontent(@PathVariable("title")String title){
        List<Shicitabel> slist= shiciservice.findBycontent2(title);
        Map<String,Object> data = new HashMap<>();
        data.put("total",slist.size());
        data.put("items",slist);
        return result.success(data);
    }

    @PostMapping("/uploadfile")
    public String uploadshici(@RequestParam("file") MultipartFile mFile) throws IOException {
        if(mFile != null){
            // 定义存储的地址
            String folder = "D:/item/biyesheji/shici/shicivue/src/files";
            // 文件夹
            File imgFolder = new File(folder);
            // 获取文件名
            String fname = mFile.getOriginalFilename();
            // 获取文件后缀
            String ext = "." + fname.substring(fname.lastIndexOf(".")+1);
            // 获取时间字符串
            String dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now());
            // 生成新的文件名
            String newFileName = dateTimeFormatter + UUID.randomUUID().toString().replaceAll("-","") + ext;
            // 文件在本机的全路径
            File filePath = new File(imgFolder, newFileName);
            mFile.transferTo(filePath);
            String src = filePath.getName();
            return filePath.getName();
        }
        else return "";
    }
}
